package org.netty.http.server.start;

import java.net.InetSocketAddress;

import org.netty.http.server.config.HttpNettyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import io.netty.channel.ChannelFuture;

@Component
@Order(value=1)
public class HttpNettyServerStartRunner implements CommandLineRunner {

	@Value("${http.netty.host}")
    private String host;
    @Value("${http.netty.port}")
    private int port;
    
    @Autowired
    private HttpNettyServer httpNettyServer;
	    
	@Override
    public void run(String... args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(host, port);
        ChannelFuture channelFuture = httpNettyServer.bing(address);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> httpNettyServer.destroy()));
        channelFuture.channel().closeFuture().syncUninterruptibly();
    }
}
